// 自动加载默认的运费
$(function(){
    var addr_id = $('#addr_id').val();
    if (addr_id) SelectEd('addr_id', addr_id);
});

// 颜色控制
function ColorS(css){
    if ('zxzf' == css) {
        $('#zxzf').addClass("btn-primary");
        $('#hdfk').removeClass("btn-primary");
        $('#payment_method').val(0);
    }else{
        $('#hdfk').addClass("btn-primary");
        $('#zxzf').removeClass("btn-primary");
        $('#payment_method').val(1);
    }
}

// 在微信端时，跳转至选择添加收货地址方式页面
function GetWeChatAddr(){
    var JsonData = b1decefec6b39feb3be1064e27be2a9;
    window.location.href = JsonData.shop_add_address;
}

// 添加收货地址
function ShopAddAddress(){
    var JsonData = b1decefec6b39feb3be1064e27be2a9;
    var url = JsonData.shop_add_address;
    var width  = JsonData.addr_width;
    var height = JsonData.addr_height;
    var url = url;
    if (url.indexOf('?') > -1) {
        url += '&';
    } else {
        url += '?';
    }
    url += 'type=order';
    //iframe窗
    layer.open({
        type: 2,
        title: '添加收货地址',
        shadeClose: false,
        maxmin: false, //开启最大化最小化按钮
        area: [width, height],
        content: url
    });
}

// 更新收货地址
function ShopEditAddress(addr_id){
    var JsonData = b1decefec6b39feb3be1064e27be2a9;
    var url = JsonData.shop_edit_address;
    var width  = JsonData.addr_width;
    var height = JsonData.addr_height;
    var url = url;
    if (url.indexOf('?') > -1) {
        url += '&';
    } else {
        url += '?';
    }
    url += 'addr_id='+addr_id;
    //iframe窗
    layer.open({
        type: 2,
        title: '修改收货地址',
        shadeClose: false,
        maxmin: false, //开启最大化最小化按钮
        area: [width, height],
        content: url
    });
}

// 删除收货地址
function ShopDelAddress(addr_id){
    layer.confirm('是否删除收货地址？', {
        title:false,
        btn: ['是', '否'] //按钮
    }, function () {
        // 是
        var JsonData = b1decefec6b39feb3be1064e27be2a9;
        var url = JsonData.shop_del_address;

        $.ajax({
            url: url,
            data: {addr_id:addr_id,_ajax:1},
            type:'post',
            dataType:'json',
            success:function(res){
                layer.closeAll();
                if ('1' == res.code) {
                    layer.msg(res.msg, {time: 1500});
                    $("#"+addr_id+'_ul_li').remove();
                }else{
                    layer.msg(res.msg, {time: 2000});
                }
            }
        });
    }, function (index) {
        // 否
        layer.closeAll(index);
    });
}

// 选中收货地址
function SelectEd(idname, addr_id, addrData) {
    if (addr_id) {
        $('#'+idname).val(addr_id);

        if (addrData && $('#addr_consignee')) {
            $('#addr_consignee').html(addrData.consignee);
            $('#addr_mobile').html(addrData.mobile);
            $('#addr_Info').html(addrData.Info);
            $('#addr_address').html(addrData.address);
        } else {
            var lis = $('#UlHtml li');
            var id  = addr_id+'_ul_li';
            $('#'+id).addClass("selected");
            lis.each(function(){
                if (id != this.id) $('#'+this.id).removeClass("selected");
            });
        }

        // 查询运费
        var JsonData = b1decefec6b39feb3be1064e27be2a9;
        var url = JsonData.shop_inquiry_shipping;
        $.ajax({
            url : url,
            data: {addr_id: addr_id, _ajax: 1},
            type:'post',
            dataType:'json',
            success:function(res){
                // 运费
                $('#template_money').html('￥'+res.data);
                
                // 计算总价+运费
                var TotalAmount_old = $('#TotalAmount_old').val();
                var AmountNew = (Number(TotalAmount_old) + Number(res.data)).toFixed(2);
                $('#TotalAmount, #PayTotalAmountID').html(parseFloat(AmountNew));

                // 计算支付后剩余余额
                var UsersMoney = (Number(JsonData.UsersMoney) - Number(AmountNew)).toFixed(2);
                $('#UsersSurplusMoneyID').html(parseFloat(UsersMoney));
            }
        });
    }
}

// 提交订单
function ShopPaymentPage(){
    layer_loading('<font id="loading_tips_230111">正在处理</font>');
    var timer = setTimeout(function(){
        var JsonData = b1decefec6b39feb3be1064e27be2a9;
        var url = JsonData.shop_payment_page;
        if (url.indexOf('?') > -1) {
            url += '&';
        } else {
            url += '?';
        }
        url += '_ajax=1';
        
        $.ajax({
            async: false,
            url : url,
            data: $('#theForm').serialize(),
            type:'post',
            dataType:'json',
            success:function(res) {
                clearTimeout(timer); // 清理定时任务
                if (1 == res.code) {
                    if (res.data.code && 'order_status_0' == res.data.code) { // 兼容第二套会员中心
                        SelectPayMethod_2(res.data.pay_id, res.data.pay_mark, res.data.unified_id, res.data.unified_number, res.data.transaction_type);
                    } else {
                        if (res.data.email) SendEmail_1608628263(res.data.email);
                        if (res.data.mobile) SendMobile_1608628263(res.data.mobile);
                        window.location.href = res.url;
                    }
                } else {
                    layer.closeAll();
                    if (1 == res.data.add_addr) {
                        ShopAddAddress();
                    } else if (res.data.url) { // 兼容第二套会员中心
                        layer.msg(res.msg, {icon: 5,time: 1500}, function(){
                            window.location.href = res.data.url;
                        });
                    } else {
                        layer.alert(res.msg, {icon:0, title: false, closeBtn: 0});
                    }
                }
            }
        });
    }, 100);
}

// 邮箱发送
function SendEmail_1608628263(result) {
    var ResultID = 1;
    if (result) {
        $.ajax({
            url: result.url,
            data: result.data,
            type:'post',
            dataType:'json'
        });
    }
    return ResultID;
}
 
// 手机发送
function SendMobile_1608628263(result) {
    var ResultID = 1;
    if (result) {
        $.ajax({
            url: result.url,
            data: result.data,
            type:'post',
            dataType:'json'
        });
    }
    return ResultID;
}

function goAddressList(obj) {
    var url = $(obj).data('url');
    if (url.indexOf('?') > -1) {
        url += '&';
    } else {
        url += '?';
    }
    url += 'gourl='+encodeURIComponent(window.location.href);
    window.location.href = url;
}

function changeExpressType(obj) {
    var prom_type = $(obj).data('prom_type');
    if (0 == prom_type) {
        $("#expressTypeDiv2").css('display','none');
        $("#expressType2").removeClass('on');
        $("#expressTypeDiv0").css('display','');
        $("#expressType0").addClass('on');
    }else if (2 == prom_type){
        $("#expressTypeDiv0").css('display','none');
        $("#expressType0").removeClass('on');
        $("#expressTypeDiv2").css('display','');
        $("#expressType2").addClass('on');
    }
}

function ChoosePickUp(type) {
    if ('show' == type) {
        var geolocation = new BMapGL.Geolocation();
        geolocation.getCurrentPosition(function(r){
            // alert('您的位置：' + r.point.lng + ',' + r.point.lat);
            var JsonData = b1decefec6b39feb3be1064e27be2a9;
            var url = JsonData.pich_up_list_url;
            $.ajax({
                url: url,
                data: {lat:r.point.lat,lng:r.point.lng,_ajax:1},
                type:'post',
                dataType:'json',
                success:function(res){
                    console.log(JsonData.is_wap)
                    if (1 == res.code) {
                        var html = '';
                        if (1 == JsonData.is_wap) {
                            // mobile
                            res.data.data.forEach(function( val, index ) {
                                html += '<div class="el-card__body" style="padding: 0px; margin-bottom: 1px;" onclick="confirm_pick_up_mobile(this);" data-title="'+val.title+'" data-address="'+val.title+'" data-time="'+val.bussiness_time+'" data-id="'+val.id+'">\n' +
                                    '                        <div style="padding: 5px;">\n' +
                                    '                            <div class="bottom card-header">\n' +
                                    '                                <span class="time">'+val.title+'</span> <br>\n' +
                                    '                                <span class="time">'+val.address+'</span> \n' +
                                    '                            </div>\n' +
                                    '                        </div>\n' +
                                    '                    </div>';
                            });
                        }else{
                            // pc
                            res.data.data.forEach(function( val, index ) {
                                html += '<tr class="el-table__row" onclick="confirm_pick_up(this);" data-title="'+val.title+'" data-address="'+val.title+'" data-time="'+val.bussiness_time+'" data-id="'+val.id+'">\n' +
                                    '                                <td rowspan="1" colspan="1">\n' +
                                    '                                    <div class="cell">\n' +
                                    '                                        <label role="radio" tabindex="0" class="el-radio">\n' +
                                    '                                            <span class="el-radio__input ">\n' +
                                    '                                                <span class="el-radio__inner"></span>\n' +
                                    '                                                <input type="radio" aria-hidden="true" tabindex="-1" class="el-radio__original">\n' +
                                    '                                            </span>\n' +
                                    '                                            <span class="el-radio__label"><i></i>\n' +
                                    '                                            </span>\n' +
                                    '                                        </label>\n' +
                                    '                                    </div>\n' +
                                    '                                </td>\n' +
                                    '                                <td rowspan="1" colspan="1">\n' +
                                    '                                    <div class="cell">'+val.title+'</div>\n' +
                                    '                                </td>\n' +
                                    '                                <td rowspan="1" colspan="1">\n' +
                                    '                                    <div class="cell">'+val.address+'</div>\n' +
                                    '                                </td>\n' +
                                    '                                <td rowspan="1" colspan="1">\n' +
                                    '                                    <div class="cell">'+val.bussiness_time+'</div>\n' +
                                    '                                </td>\n' +
                                    '                            </tr>';
                            });
                        }
                        $("#pick_up_tbody").html(html);
                    } else {
                        layer.msg(res.msg, {time: 1500, icon: 2});
                    }
                },
                error : function() {
                    layer.alert('网络失败，请刷新页面后重试', {icon: 5, closeBtn: 0});
                }
            });
        });

        $("#PickUpMask0").show();
        $("#PickUpMask1").show();
    }else{
        $("#PickUpMask0").hide();
        $("#PickUpMask1").hide();
    }
}

function confirm_pick_up(obj) {
    var html = $(obj).data('title');
    // var html = $(obj).data('title') + $(obj).data('address') + $(obj).data('time');
    $("#expressTypeDiv2 .pickupinfo").html(html);
    $("#points_id").val($(obj).data('id'));
    ChoosePickUp('hide');
}

function confirm_pick_up_mobile(obj) {
    var html = $(obj).data('title');
    // var html = $(obj).data('title') + $(obj).data('address') + $(obj).data('time');
    $("#verifyMobile").html(html);
    $("#points_id").val($(obj).data('id'));
    ChoosePickUp('hide');
}